local textRadio = ""
local currentRadio = ""
local currentIndex = 1
local localPlayer = getLocalPlayer()
local sx,sy = guiGetScreenSize ()
local loltimer = nil
local templol = false
local radio_font = dxCreateFont("FILES/HUD_FONT.TTF", 19)
local radio_font_info = dxCreateFont("FILES/HUD_FONT.TTF", 10)

local car_radio_plays = {}

BUTTERFLY_RADIO = {"Radio aus",
  --[["Playback FM",
  "K-Rose",
  "K-DST",
  "Bounce FM",
  "SF-UR",
  "Radio Los Santos",
  "Radio X",
  "CSR 103.9",
  "K-Jah West",
  "Master Sounds 98.3",
  "WCTR",
  "User Track Player",]] 
  "Di.fm Dubstep",
  "Di.fm Electro House",
  "RMF Dance",
  "Sky.fm Alternative",
  "Sky.fm Roots Reggae",
  "Sky.fm Classic Rap",
  "Sky.fm Top Hits",
  "89.0 RTL",
  "Technobase.fm",
  "N-Joy",
  "Hardbase.fm",
  "Housetime.fm",
  "Techno4Ever",
  "ClubTime.fm",
  "CoreTime.fm",
}

BUTTERFLY_RADIO_PATHS = {0,--1,2,3,4,5,6,7,8,9,10,11,12,
  "http://80.94.69.106:6374/",
  "http://scfire-ntc-aa02.stream.aol.com:80/stream/1025",
  "http://files.kusmierz.be/rmf/rmfdance-3.mp3",
  "http://u12.sky.fm:80/sky_altrock_aacplus",
  "http://u16b.sky.fm:80/sky_rootsreggae_aacplus",
  "http://u17.sky.fm:80/sky_classicrap_aacplus",
  "http://u12b.sky.fm:80/sky_tophits_aacplus",
  "http://80.237.156.45/890rtl-128.mp3",
  "http://listen.technobase.fm/dsl.asx",
  "http://ndrstream.ic.llnwd.net/stream/ndrstream_n-joy_hi_mp3.m3u",
  "http://listen.hardbase.fm/tunein-dsl-asx",
  "http://listen.housetime.fm/tunein-dsl-asx",
  "http://www.techno4ever.net/t4e/stream/dsl_listen.asx",
  "http://listen.ClubTime.fm/dsl.pls",
  "http://listen.CoreTime.fm/dsl.pls"
}

local playRadioThing = nil

function resetTimer ()
  textRadio = ""
end

addEventHandler("onClientResourceStart",getResourceRootElement(),
function()
  triggerServerEvent("radio_load_success", getRootElement())
  
  showPlayerHudComponent ("radio", false)
  setRadioChannel (0)
  
	bindKey("x", "down", function(key,state)
	
	local radio = BUTTERFLY_RADIO_PATHS[1]
    textRadio = BUTTERFLY_RADIO[1]
    if loltimer and isTimer (loltimer) then
      killTimer (loltimer)
    end
    loltimer = setTimer (resetTimer,3000,1)
    if type (radio) == "number" then
      setRadioChannel (radio)
      if playRadioThing then
        stopSound (playRadioThing)
      end
    else
      setRadioChannel (0)
      if playRadioThing then 
        stopSound (playRadioThing)
      end
      playRadioThing = playSound (radio)
	  if getPedOccupiedVehicle(getLocalPlayer()) then
		 setElementData(getPedOccupiedVehicle(getLocalPlayer()), "now_radio_played", radio)
		 triggerServerEvent("car_radio_changed", getRootElement(), getPedOccupiedVehicle(getLocalPlayer()))
	  end
	  --triggerServerEvent("CREATE_OUTSIDE_RADIO", getRootElement(), radio, source)
	  end
  
end)
  
  bindKey ("radio_next","down",
    function(key,state)
	
      local nextIndex = ((currentIndex)%(#BUTTERFLY_RADIO_PATHS)) +1
      currentIndex = nextIndex
      local radio = BUTTERFLY_RADIO_PATHS[nextIndex]
      textRadio = BUTTERFLY_RADIO[nextIndex]
      if loltimer and isTimer (loltimer) then
        killTimer (loltimer)
      end
      loltimer = setTimer (resetTimer,1000,1)
      if type (radio) == "number" then
        setRadioChannel (radio)
        if playRadioThing then
          stopSound (playRadioThing)
        end
      else
        setRadioChannel (0)
        if playRadioThing then 
          stopSound (playRadioThing)
        end
        playRadioThing = playSound (radio)
		if getPedOccupiedVehicle(getLocalPlayer()) then
			setElementData(getPedOccupiedVehicle(getLocalPlayer()), "now_radio_played", radio)
			triggerServerEvent("car_radio_changed", getRootElement(), getPedOccupiedVehicle(getLocalPlayer()))
		end
      end
	 
    end
  )
  
  bindKey ("radio_previous","down",
    function(key,state)
	
      local nextIndex = ((currentIndex -2)%(#BUTTERFLY_RADIO_PATHS)) +1
      currentIndex = nextIndex
      local radio = BUTTERFLY_RADIO_PATHS[nextIndex]
      textRadio = BUTTERFLY_RADIO[nextIndex]
      if loltimer and isTimer (loltimer) then
        killTimer (loltimer)
      end
      loltimer = setTimer (resetTimer,1000,1)
      if type (radio) == "number" then
        setRadioChannel (radio)
        if playRadioThing then
          stopSound (playRadioThing)
        end
      else
        setRadioChannel (0)
        if playRadioThing then 
          stopSound (playRadioThing)
        end
        playRadioThing = playSound (radio)
		if getPedOccupiedVehicle(getLocalPlayer()) then
			setElementData(getPedOccupiedVehicle(getLocalPlayer()), "now_radio_played", radio)
			triggerServerEvent("car_radio_changed", getRootElement(), getPedOccupiedVehicle(getLocalPlayer()))
		end
      end
	 
    end
  )
  
end)

function renderRadio ()
  dxDrawText (textRadio,0,0,sx - 1,96 - 1,tocolor(0, 0, 0),1, radio_font,"center","center",false,false,true)
  dxDrawText (textRadio,0,0,sx,96,tocolor(211, 211, 211),1, radio_font,"center","center",false,false,true)
end
addEventHandler ("onClientRender",getRootElement(), renderRadio)

addEventHandler ("onClientVehicleStartExit",getRootElement(),
function(player)
  if localPlayer == player then
    if playRadioThing then
      stopSound (playRadioThing)
    end
    setRadioChannel (0)
    textRadio = ""
  end
end)

addEventHandler ("onClientVehicleExit",getRootElement(),
function(player)
  if localPlayer == player then
    if playRadioThing then
      stopSound (playRadioThing)
    end
    setRadioChannel (0)
    textRadio = ""
	setSoundVolume(car_radio_plays[source], 1)
  end
end)

addEventHandler ("onClientVehicleEnter",getRootElement(),
function(player)
  if localPlayer == player then
    local radio = BUTTERFLY_RADIO_PATHS[currentIndex]
    textRadio = BUTTERFLY_RADIO[currentIndex]
    if loltimer and isTimer (loltimer) then
      killTimer (loltimer)
    end
    loltimer = setTimer (resetTimer,3000,1)
    if type (radio) == "number" then
      setRadioChannel (radio)
      if playRadioThing then
        stopSound (playRadioThing)
      end
    else
      setRadioChannel (0)
      if playRadioThing then 
        stopSound (playRadioThing)
      end
      playRadioThing = playSound (radio)
	  --triggerServerEvent("CREATE_OUTSIDE_RADIO", getRootElement(), radio, source)
	  setSoundVolume(car_radio_plays[source], 0)
    end
	addEventHandler("onClientRender", getRootElement(), RENDER_OFF_X_INFO)
	setTimer(function() removeEventHandler("onClientRender", getRootElement(), RENDER_OFF_X_INFO) end, 4000, 1)
  end
end)

_setRadioChannel = setRadioChannel

function setRadioChannel (index)
  templol = true
  _setRadioChannel (index)
end

addEventHandler ("onClientPlayerRadioSwitch",getRootElement(),
function()
  if templol == true then
    if not isPedInVehicle (localPlayer) then
      setRadioChannel (0)
    end
    templol = false
  else
    cancelEvent (true)
    if not isPedInVehicle (localPlayer) then
      setRadioChannel (0)
    end
  end
end)

addEventHandler("onClientElementDestroy",getRootElement(),
function()
  if source and getElementType (source) == "vehicle" then
    if source == getPedOccupiedVehicle (localPlayer) then
      if playRadioThing then
        stopSound (playRadioThing)
      end
      setRadioChannel (0)
      textRadio = ""
    end
  end
end)

addEventHandler("onClientVehicleExplode",getRootElement(),
function()
  if source and getElementType (source) == "vehicle" then
    if source == getPedOccupiedVehicle (localPlayer) then
      if playRadioThing then
        stopSound (playRadioThing)
      end
      setRadioChannel (0)
      textRadio = ""
    end
  end
end)

function RENDER_OFF_X_INFO()
	dxDrawText("INFO: Mit 'x' kannst du sofort das Radio abstellen!", 0,0,sx,150, tocolor(0, 0, 0), 1, radio_font_info, "center", "center", false, false, false)
	dxDrawText("INFO: Mit 'x' kannst du sofort das Radio abstellen!", 0,0,sx,150, tocolor(211, 211, 211), 1, radio_font_info, "center", "center", false, false, false)
end

addEvent("play_car_radio_out", true)
addEventHandler("play_car_radio_out", getRootElement(), function(url, veh)
	if getPedOccupiedVehicle(getLocalPlayer()) == veh then
		local x, y, z = getElementPosition(veh)
		stopSound(car_radio_plays[veh])
		car_radio_plays[veh] = playSound3D(url, x, y, z)
		setSoundEffectEnabled(car_radio_plays[veh], "i3dl2reverb", true)
		attachElements(car_radio_plays[veh], veh)
		setSoundVolume(car_radio_plays[veh], 0)
		setSoundMaxDistance(car_radio_plays[veh], 30)
	else
		if car_radio_plays[veh] then
			local x, y, z = getElementPosition(veh)
			stopSound(car_radio_plays[veh])
			car_radio_plays[veh] = playSound3D(url, x, y, z)
			setSoundEffectEnabled(car_radio_plays[veh], "i3dl2reverb", true)
			attachElements(car_radio_plays[veh], veh)
			setSoundMaxDistance(car_radio_plays[veh], 30)
		else
			local x, y, z = getElementPosition(veh)
			car_radio_plays[veh] = playSound3D(url, x, y, z)
			setSoundEffectEnabled(car_radio_plays[veh], "i3dl2reverb", true)
			attachElements(car_radio_plays[veh], veh)
			setSoundMaxDistance(car_radio_plays[veh], 30)
		end
	end
end)